package com.beagle.njddapp.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.InvateBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.utils.QRCodeUtil;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.utils.WxShareUtils;
import com.beagle.njddapp.weight.dialog.ShareDialog;

import java.io.ByteArrayOutputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.beagle.njddapp.core.UserService.ROLE_DIRVER;
import static com.beagle.njddapp.core.UserService.ROLE_OWNER;

/**
 * @author hxx
 * @date 2021-06-23 11:39
 */
public class InvateActivity extends AppCompatActivity implements ShareDialog.shareDialogHandleCallback {

    @BindView(R.id.common_back_iv)
    ImageView ivBack;
    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.iv_qr)
    ImageView ivQrcode;
    @BindView(R.id.tv_role)
    TextView tvRole;
    @BindView(R.id.common_container)
    View container;
    private ApiService apiService;
    private Bitmap bitmap;
    private ShareDialog shareDialog;
    private static String h5="https://api.sinagin.com/njdd/h5/#/?phone=";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invate);
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, container);
        ivBack.setImageResource(R.mipmap.icon_back_white);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("分享");
        tvRight.setTextColor(Color.WHITE);
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (shareDialog==null){
                    shareDialog=new ShareDialog(InvateActivity.this);
                    shareDialog.setShareDialogHandleCallback(InvateActivity.this);
                }
                shareDialog.show();
            }
        });
        tvTitle.setText("我的邀请码");
        tvTitle.setTextColor(Color.WHITE);
        tvName.setText(UserService.getInstance().getUserInfoBean().getUserName());
        if (UserService.getInstance().getRole() == ROLE_OWNER) {
            tvRole.setText("农机主");
        } else if (UserService.getInstance().getRole() == ROLE_DIRVER) {
            tvRole.setText("农机驾驶员");
        } else {
            tvRole.setText("加油车驾驶员");
        }
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        apiService = retrofit.create(ApiService.class);
        apiService.inviteeCount().compose(RxSchedulers.compose()).subscribe(new Observer<InvateBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(InvateBean invateBean) {
                if (invateBean != null) {
                    tvCount.setText("已成功邀请人数：" + invateBean.getResult() + "人");
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        bitmap = QRCodeUtil.createQRCodeBitmap(h5 + UserService.getInstance().getPhone(), 400, 400);
        ivQrcode.setImageBitmap(bitmap);

    }
    public static Bitmap saveBitmapToFileWithCompress(Bitmap bitmap,

                                                      final int maxSize) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        int scale = 100;

        if (bitmap != null) {

            if (bitmap.compress(Bitmap.CompressFormat.JPEG, scale, baos)) {

                int baosSize = baos.toByteArray().length;

                while (baosSize > maxSize && scale > 0) {

                    baos.reset();

                    bitmap.compress(Bitmap.CompressFormat.JPEG, scale, baos);

                    baosSize = baos.toByteArray().length;

                    scale -= 5;

                }
                return bitmap;

            }

        }
        return null;


    }


    @Override
    public void cicler() {
        WxShareUtils.shareWebtoCicler(getApplicationContext(),"wx3d87e26691a0338a",h5 + UserService.getInstance().getPhone(),"中农智慧，一键加油，免费送达","您的好友邀您加入中农智慧，为您的农机享受上门加油服务！赶快试试吧！",saveBitmapToFileWithCompress(BitmapFactory.decodeResource(getResources(),R.drawable.app_icon),32));
    }

    @Override
    public void friend() {
        WxShareUtils.shareWebtoFriend(getApplicationContext(),"wx3d87e26691a0338a",h5 + UserService.getInstance().getPhone(),"中农智慧，一键加油，免费送达","您的好友邀您加入中农智慧，为您的农机享受上门加油服务！赶快试试吧！",saveBitmapToFileWithCompress(BitmapFactory.decodeResource(getResources(),R.drawable.app_icon),32));

    }
}
